$(() => {
	loadData();
	initListener();
})

initListener = () => {
	$(".form_input").bind('input propertychange', (event) => {
		let value = event.target.value;
		if (value && value.length > 0) {
			$(`.icon_clear`).show(300);
			onHandleSearch(value);
		} else {
			$(`.icon_clear`).hide();
		}
	});
	$(".icon_clear").on('click', () => {
		$(`.icon_clear`).hide();
		$(`.form_input`).val('');
		loadData();
	});
	$("#load_layout").on('click', () => {
		loadData();
	});
}

onHandleSearch = (keyword) => {
	loadData();
}

loadData = () => {
	startLoading();
	$("#list").html('');
	let postData = {};
	setTimeout(() => {
		queryAfterServiceList(res => {
			hideLoading();
			if (res.code == 200) {
				setList(res.data, 'list');
			} else {
				setLoadState('stateUlr', loginFail());
			}
		})
	}, 500)
}

setList = (mlist, listId) => {
	$(`#${listId}`).html('');
	let itemHtml = ``;
	for (var i = 0; i < mlist.length; i++) {
		let obj = mlist[i];
		let orderNo = obj.orderNo;
		itemHtml += `<div class="order_item">
						<div class="order_header">
							<img src="../../img/ic_shop.png" class="icon_20" />
							<span class="flex_1 m_left_com">${obj.storeName}</span>
							<img src="../../img/ic_return_goods.png" class="icon_14 m_left_com" />
							<span class="order_status">${obj.bizRightsStatusName}</span>
						</div>`;
		let orderItemVOs = obj.orderItemVOs;
		for (var j = 0; j < orderItemVOs.length; j++) {
			let childObj = orderItemVOs[j];

			itemHtml += `<div class="order_item_child" onclick="onViewDetail('${orderNo}')">
							<img src="${childObj.goodsPictureUrl}" class="order_img" />
							<div class="order_content">
								<span class="title">${childObj.goodsName}</span>
								<span class="des">${childObj.specInfo[0].specValues},${childObj.specInfo[1].specValues}</span>
								<div class="list_item_child" style="justify-content: space-between;margin-top: 0.6rem;">
									<div class="goods_price">
										<span class="unit">¥</span>
										<span class="price">${childObj.actualPrice}</span>
									</div>
									<div class="goods_quantity">
										<span class="unit">X</span>
										<span class="num">${childObj.itemRefundAmount}</span>
									</div>
								</div>
							</div>
						</div>`;
		}
		let orderStatus = obj.userRightsStatus;
		let btnItemHtml = ``;
		if (orderStatus == 100) {
			btnItemHtml = `<div class="order_option">
								<button class="btn_cancel" onclick="onModifyLogistics('${orderNo}')">修改运单号</button>
								<button class="btn_cancel" onclick="onViewLogistics('${orderNo}')">查看物流</button>
							</div>`;
		} else if (orderStatus == 110) {
			btnItemHtml = `<div class="order_option">
								<button class="btn_cancel" onclick="onModifyLogistics('${orderNo}')">填写运单号</button>
							</div>`;
		} else if (orderStatus == 120) {
			btnItemHtml = `<div class="order_option">
								<button class="btn_cancel" onclick="onCancelApply('${orderNo}')">撤销申请</button>
							</div>`;
		} else if (orderStatus == 130) {
			btnItemHtml = `<div class="order_option">
								<button class="btn_cancel" onclick="onModifyLogistics('${orderNo}')">修改运单号</button>
							</div>`;
		}

		itemHtml += `<div class="order_tips">${obj.userRightsStatusDesc}</div>
						${btnItemHtml}
					</div>`;
	}
	$(`#${listId}`).html(itemHtml);
}

onViewDetail = (id) => {
	navigateTo(`after_service_detail.html?orderNo=${id}`)
}

onModifyLogistics = (id) => {
	navigateTo(`edit_bill_no.html?orderNo=${id}`)
}

onViewLogistics = (id) => {
	navigateTo(`delivery_detail.html?orderNo=${id}`)
}

onCancelApply = (id) => {
	showActionDialog('', `您确定要撤销申请吗？`, res => {
		let subLoading = layer.open({
			type: 2,
			shadeClose: false,
			content: '正在撤销...'
		});
		setTimeout(() => {
			layer.close(subLoading);
			showToast('提交成功！');
			loadData();
		}, 500)
	})
}
